package com.dhwl.hsh.driver.ui.adapter;

import android.content.Context;
import android.content.res.TypedArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dhwl.hsh.driver.R;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 简要描述
 *
 * @author Derek.Kidd
 * @version V1.0.0
 * @date 2016-04-28 17:48
 */
public class TruckListAdapter extends BaseAdapter {

    TypedArray iconTruck;
    String[] titleArr;
    String[] infoArr;

    int checkNum = 0;

    public TruckListAdapter(Context context) {
        super(context);
        iconTruck = context.getResources().obtainTypedArray(R.array.truck_icon);
        titleArr = context.getResources().getStringArray(R.array.truck_title);
        infoArr = context.getResources().getStringArray(R.array.truck_info);
    }

    @Override
    public int getCount() {
        return titleArr.length;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_truck_list, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.iconLeftTruck.setImageResource(iconTruck.getResourceId(position,0));
        viewHolder.tvTruckTitle.setText(titleArr[position]);
        viewHolder.tvTruckInfo.setText(infoArr[position]);

        if(checkNum == position){
            viewHolder.layoutTruck.setPressed(true);
            viewHolder.iconLeftTruck.setPressed(true);
            viewHolder.checkBoxItemTruck.setVisibility(View.VISIBLE);
        }else{
            viewHolder.layoutTruck.setPressed(false);
            viewHolder.iconLeftTruck.setPressed(false);
            viewHolder.checkBoxItemTruck.setVisibility(View.INVISIBLE);
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkNum = position;
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    public int getCheckNum() {
        return checkNum;
    }

    static class ViewHolder {
        @Bind(R.id.icon_left_truck)
        ImageView iconLeftTruck;
        @Bind(R.id.layout_truck)
        RelativeLayout layoutTruck;
        @Bind(R.id.tv_truck_title)
        TextView tvTruckTitle;
        @Bind(R.id.tv_truck_info)
        TextView tvTruckInfo;
        @Bind(R.id.checkBox_item_truck)
        CheckBox checkBoxItemTruck;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
